package Fragm;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.gson.Gson;
import com.xm.pc.mildsnews.R;

import java.util.ArrayList;

import Bean.NewsCategoryRoot;
import Xlistviews.XListView;
import adapter.NewsCategroyAdapter;
import base.BaseData;
import base.BaseFragment;
import utils.URLUtils;

public class MainFragment extends BaseFragment implements XListView.IXListViewListener{

    private XListView mListView;
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> items = new ArrayList<String>();
    private Handler mHandler;
    private int start = 0;
    private static int refreshCnt = 0;
    private NewsCategroyAdapter newsCategroyAdapter;
    private ListView listView;

    @Override
    public View initView(LayoutInflater inflater) {
//        textView = new TextView(getActivity());
        View view = inflater.inflate(R.layout.fragment_main, null);
        listView = (ListView) view.findViewById(R.id.lv_fragment_main);
        return view;
    }
   /* @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
      View v =  inflater.inflate(R.layout.fragment_item, container, false);
        initData();
        mListView = (XListView) v.findViewById(R.id.xListView);
        mListView.setPullLoadEnable(true);
        mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, items);
        mListView.setAdapter(newsCategroyAdapter);
        mListView.setXListViewListener(this);
        mHandler = new Handler();
        return v;

    }*/



    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        mListView.setRefreshTime("就在刚才啊");
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                start = ++refreshCnt;
                items.clear();
                initData();
                // mAdapter.notifyDataSetChanged();
                mAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item, items);
                mListView.setAdapter(newsCategroyAdapter);
                onLoad();
            }
        }, 2000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                initData();
                newsCategroyAdapter.notifyDataSetChanged();
                onLoad();
            }
        }, 2000);
    }
    @Override
    protected void initData() {
        String category = getArguments().getString("category");

        BaseData baseData=new BaseData() {
            @Override
            public void setResultData(String data) {
                showData(data);
            }

            @Override
            protected void setFailResult(int error_Net) {

            }
        };
        baseData.getData(URLUtils.URL_CATEGROY_PATH+category,BaseData.LONGTTIME);

    }
    private void showData(String data) {

        Gson gson = new Gson();
        NewsCategoryRoot newsCategoryRoot = gson.fromJson(data, NewsCategoryRoot.class);
        //展示
        ArrayList<NewsCategoryRoot.DataBean> dataBeanList = (ArrayList<NewsCategoryRoot.DataBean>) newsCategoryRoot.getData();
        newsCategroyAdapter = new NewsCategroyAdapter(getActivity(), dataBeanList);
        listView.setAdapter(newsCategroyAdapter);

    }
    public static Fragment getInstance(String category){
        MainFragment mainFragment = new MainFragment();
        Bundle bundle=new Bundle();
        bundle.putString("category",category);
        mainFragment.setArguments(bundle);
        return  mainFragment;
    }
}
